package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.exceptionResolver.ExceptionMessage;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkItemGroup")
public class CheckGroupController {
    @Reference
    CheckGroupService checkGroupService;

    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('CHECKGROUP_ADD')")
    @ExceptionMessage(value = MessageConstant.ADD_CHECKGROUP_FAIL)
    public Result add(@RequestBody CheckGroup checkGroup, Integer[] checkItemIds) {
            checkGroupService.add(checkGroup, checkItemIds);
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("findPage")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    @ExceptionMessage(value = MessageConstant.QUERY_CHECKGROUP_FAIL)
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        int a=2/0;
        return checkGroupService.findPage(queryPageBean);
    }

    @RequestMapping("/deleteById/{id}")
    @PreAuthorize("hasAuthority('CHECKGROUP_DELETE')")
    @ExceptionMessage(value = MessageConstant.DELETE_CHECKGROUP_FAIL)
    public Result deleteById(@PathVariable("id") int id){
            checkGroupService.deleteById(id);
        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    @RequestMapping("/findGroupById/{id}")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    @ExceptionMessage(value = MessageConstant.QUERY_CHECKGROUP_FAIL)
    public Result findGroupById(@PathVariable("id") int id){
        CheckGroup checkGroup=null;
            checkGroup=checkGroupService.findGroupById(id);
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
    }

    @RequestMapping("/update")
    @PreAuthorize("hasAuthority('CHECKGROUP_EDIT')")
    @ExceptionMessage(value = MessageConstant.ADD_CHECKGROUP_FAIL)
    public Result update(@RequestBody CheckGroup checkGroup, Integer[] checkItemIds) {
            checkGroupService.update(checkGroup, checkItemIds);
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @RequestMapping("findAllGroup")
    @PreAuthorize("hasAuthority('CHECKGROUP_QUERY')")
    @ExceptionMessage(value = MessageConstant.QUERY_CHECKGROUP_FAIL)
    public Result findAllGroup(){
        List<CheckGroup> checkGroups= null;
            checkGroups = checkGroupService.findAllGroup();
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroups);
    }
}
